import { show_loading, stop_loading } from "./ditto";
import { pregohome } from "./pregohome";
import { resolvemdpathfromurl } from "./resolvemdpathfromurl";
import { states } from "./states";

export default async function loadpage() {
    const { cachemain } = states;
    // const appvm = getappvm();
    // console.log(cachemain);
    // window.scrollTo(0, 0);
    show_loading();
    const path: string = resolvemdpathfromurl();

    const marktext = cachemain.get(path);
    const { cachetoc } = states;
    const toc = cachetoc.get(path);
    const url = path;
    if (marktext && toc) {
        // contenthtml.set(marktext);
        stop_loading();
        // Reflect.set(appvm, "showsrc", true);
        // addmathjaxstyle(path);
        return;
    } else {
        if (!states.menulist.includes(url)) {
            // console.log(Object.keys(hashrouter.getparams()))

            if (location.hash.length >= 1) {
                requestAnimationFrame(() => {
                    setTimeout(() => {
                        console.error(
                            "Opps! ... File not found!\n5秒后返回主页"
                        );
                        pregohome();
                    }, 1000);
                });
                return;
            }
            return;
        }
        if (!process.env.PRERENDER) {
            const { default: csrloadpage } = await import("./csrloadpage");
            await csrloadpage();
        } else {
        }
    }
    stop_loading();
    // } else {
    //     stop_loading();
    //     return;
    // }
}
